package jp.sourceforge.akjrcp.ui.param;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

public class ParamRadioContainer extends AbstractParamItem{
private String key;


private String value="";//setted value
List<Button> buttons=new ArrayList<Button>();
Map<Button,String> valueMap=new HashMap<Button,String>();
public String getKey() {
	return key;
}

public void setToolTipText(String text){

	for (int i = 0; i < buttons.size(); i++) {
		buttons.get(i).setToolTipText(text);
	}
}



public String getValue(){
	String ret="";
	for (int i = 0; i < buttons.size(); i++) {
		Button bt=buttons.get(i);
		if(bt.getSelection()){
			 return valueMap.get(bt);	//return selection 
		}
	}
	return ret;
}

public void setValue(String value) {
	for (int i = 0; i < buttons.size(); i++) {
		Button bt=buttons.get(i);
		String btKey=valueMap.get(bt);
			if(btKey.equals(value)){
				bt.setSelection(true);
			}else{
				bt.setSelection(false);
			}
	}
	this.value = value;
	
}

private SelectionCheck selectionCheck=new SelectionCheck();

	public ParamRadioContainer(String key) {
		this.key=key;
	}
	

	public Button createButton(Composite parent,int layout,String label,String v){
		Button radio=new Button(parent,SWT.RADIO|layout);
		if(buttons.size()==0){
			radio.setSelection(true);//default index 0 selected
			if(v!=null){
			this.value=v;	
			}else{
			this.value=label;
			}
		}
		radio.addSelectionListener(selectionCheck);
		radio.setText(label);
		buttons.add(radio);
		if(v!=null){
				valueMap.put(radio,v);
			}else{
				valueMap.put(radio,label);
			}
		
		return radio;
	}
	
	/**
	 * don't re set label
	 * @param parent
	 * @param layout
	 * @param label
	 * @param v
	 * @return
	 */
	public Button createButton(Composite parent,int layout,String label){
		return createButton(parent,layout,label,null);
	}


	
	public boolean isModified() {
		String newValue=getValue();
		return !value.equals(newValue);
	}

}
